package com.shadow.bus.main.setting;

import android.content.Context;
import android.widget.RadioGroup;

import com.shadow.bus.config.ItemFiled;
import com.shadow.bus.config.ItemType;
import com.shadow.bus.config.SpKey;
import com.tepth.shadow.ui.recycler.MultipleItemEntity;
import com.tepth.shadow.ui.recycler.MultipleRecyclerAdapter;
import com.tepth.shadow.ui.recycler.MultipleViewHolder;
import com.zhangyu.shadow.core.util.storage.ShadowPreference;

import java.util.List;

import busfix.tepth.com.shadow_bus.R;

/**
 * @author zhangyu
 * @date 2018/7/3
 */

public class ItemAdapter extends MultipleRecyclerAdapter {
    private final Context mContext;

    protected ItemAdapter(List<MultipleItemEntity> data, Context context) {
        super(data);
        mContext = context;
        addItemType(ItemType.TYPE_SETTING_TV, R.layout.item_setting_tv);
        addItemType(ItemType.TYPE_SETTING_CARD, R.layout.item_setting_card);
        addItemType(ItemType.TYPE_SETTING_BUTTON, R.layout.item_setting_button);
        addItemType(ItemType.TYPE_SETTING_RADIO_GROUP, R.layout.item_setting_radio_group);
    }

    @Override
    protected void convert(MultipleViewHolder helper, MultipleItemEntity item) {
        switch (item.getItemType()) {
            case ItemType.TYPE_SETTING_TV:
                helper.setText(R.id.str_account, (CharSequence) item.getField(ItemFiled.FILED_NAME));
                helper.setText(R.id.tv_account, (CharSequence) item.getField(ItemFiled.FILED_VALUE));
                break;
            case ItemType.TYPE_SETTING_CARD:
                helper.setText(R.id.str_account, (CharSequence) item.getField(ItemFiled.FILED_NAME));
                helper.setText(R.id.tv_account, (CharSequence) item.getField(ItemFiled.FILED_VALUE));
                break;
            case ItemType.TYPE_SETTING_BUTTON:
                helper.setText(R.id.button, (CharSequence) item.getField(ItemFiled.FILED_NAME));
                break;
            case ItemType.TYPE_SETTING_RADIO_GROUP:
                RadioGroup radioGroup = helper.getView(R.id.radio_group);
                helper.setText(R.id.tv_name, (CharSequence) item.getField(ItemFiled.FILED_NAME));
                boolean isCard = item.getField(ItemFiled.FILED_VALUE);
                radioGroup.check(isCard ? R.id.radio_card : R.id.radio_name);
                radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        if (checkedId == R.id.radio_card) {
                            ShadowPreference.setAppFlag(SpKey.KEY_CARD_WAY, true);
                        } else {
                            ShadowPreference.setAppFlag(SpKey.KEY_CARD_WAY, false);
                        }
                    }
                });
                break;
        }
    }
}
